package com.baizhi.Controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("guru")
public class GuruController {
    @Autowired
    private GuruService guruService;
    @RequestMapping("insert")
    @DeleteCacheAnnotation
    public @ResponseBody Map insert(MultipartFile file, Guru guru,HttpServletRequest request) throws IOException {
        //获得绝对路径
        System.out.println(guru+"上师========================");
        String realPath=request.getServletContext().getRealPath("/img");
        /*System.out.println(realPath+"===========绝对路径");*/
        //通过uuid获得新名字
        if(file!=null) {
            String newFileName = new Date().getTime() + "_" + file.getOriginalFilename();
            /* System.out.println(newFileName+"========文件名字");*/
            file.transferTo(new File(realPath + "/" + newFileName));
            guru.setGuruImage("/img/" + newFileName);
        }
        Map map=guruService.insertGuru(guru);
        return  map;
    }
    @RequestMapping("delete")
    @DeleteCacheAnnotation
    public @ResponseBody Map delete(int guruId){
        Map map=guruService.deleteGuruById(guruId);
        return map;
    }
    @RequestMapping("update")
    @DeleteCacheAnnotation
    public @ResponseBody Map update(MultipartFile file,Guru guru,HttpServletRequest request) throws IOException {
        //获得绝对路径
        System.out.println(guru+"上师========================");
        String realPath=request.getServletContext().getRealPath("/img");
        /*System.out.println(realPath+"===========绝对路径");*/
        //通过uuid获得新名字
        if(file!=null) {
            String newFileName = new Date().getTime() + "_" + file.getOriginalFilename();
            /* System.out.println(newFileName+"========文件名字");*/
            file.transferTo(new File(realPath + "/" + newFileName));
            guru.setGuruImage("/img/" + newFileName);
        }
        Map map=guruService.updateGuru(guru);
        return  map;
    }
    @RequestMapping("showAll")
    @AddCacheAnnotation
    public @ResponseBody Map showAll(int page,int limit){
        Map map=guruService.showAll(page,limit);
        return map;
    }
    @RequestMapping("selectOne")
    @AddCacheAnnotation
    public @ResponseBody Guru selectOne(int guruId){
        Guru guru=guruService.selectOne(guruId);
        return guru;
    }
    @RequestMapping("upload")
    @DeleteCacheAnnotation
    @ResponseBody
    public Map uploadExcel(MultipartFile file) throws Exception {
        Map map = new HashMap();
       /* *//*  *//**//*1.读取文件*//**//*
        FileInputStream inputStream = new FileInputStream("F://gurus.xls");*//*
        *//*2.通过poi处理流*//*
        if(file!=null) {
            HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
            // 3.取出文件对象中的数据
// 3.1取出表 getSheetAt(0)通过下标获取
            HSSFSheet sheet = workbook.getSheetAt(0);
// 3.2获取表中的行 getLastRowNum()获取最后一行的下标
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {
// 3.3获取行
                HSSFRow row = sheet.getRow(i);
// 3.4通过行得到单元格
                String guruId = row.getCell(0).getStringCellValue();
                String guruName = row.getCell(1).getStringCellValue();
                String guruImage = row.getCell(2).getStringCellValue();
                String guruNickName = row.getCell(3).getStringCellValue();
                String guruStatus = row.getCell(4).getStringCellValue();
                Guru guru = new Guru();
                guru.setGuruId(Integer.parseInt(guruId));
                guru.setGuruName(guruName);
                guru.setGuruImage(guruImage);
                guru.setGuruNickName(guruNickName);
                guru.setGuruStatus(Integer.parseInt(guruStatus));

// 3.5添加数据库
                System.out.println(guru);
                map = guruService.insertGuru(guru);
            }
        }*/
        /**
         * 导入参数对象
         * 参数一：文件或者流
         * 参数2：实体类的类队象
         * 参数三：导入参数对象
         */
        //导入参数对象
        ImportParams importParams=new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        List<Guru> gurus= ExcelImportUtil.importExcel(file.getInputStream(),Guru.class,importParams);
        for (Guru guru:gurus){
            map=guruService.insertGuru(guru);
        }
        return map;
    }
    @RequestMapping("download")
    @DeleteCacheAnnotation
    @ResponseBody
    public void guruDownload(HttpServletResponse response) throws IOException{
        /**
         * 参数1 导出参数对象 定义标题 和 表名
         * 参数2 被导出的实体类的类对象
         * 参数3 被导出的数据
         */
        ExportParams exportParams=new ExportParams("所有的上师数据","gurus");
        List<Guru> gurus=guruService.selectAll();
        Workbook workbook= ExcelExportUtil.exportExcel(exportParams,Guru.class,gurus);
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("gurus.xls", "utf-8"));
            response.setContentType("application/vnd.ms-excel");
//通过文件对象写出
            workbook.write(response.getOutputStream());

    }
}
